package com.bike.util;

import java.util.Arrays;

 public class Distance {

    private static final double EARTH_RADIUS = 6378.137;

    private static double rad(double d){
        return d * Math.PI / 180.0;
    }
    
    private final static double PI = 3.14159265358979323; //圆周率
    private final static double R = 6371229;             //地球的半径
    
    public static double getDistance(double longt1, double lat1, double longt2, double lat2){
        double x,y, distance;
        x=(longt2-longt1)*PI*R*Math.cos( ((lat1+lat2)/2)*PI/180)/180;
        y=(lat2-lat1)*PI*R/180;
        distance=Math.hypot(x,y);
        return distance;
    }
    public static long getDistance(long longt1, long lat1, long longt2, long lat2){
    	
    	 double a,b,c,d;
    	 
    	 a=TransFerUtil.getLatLngForLong(longt1);
    	 b=TransFerUtil.getLatLngForLong(lat1);
    	 c=TransFerUtil.getLatLngForLong(longt2);
    	 d=TransFerUtil.getLatLngForLong(lat2);
    	double distanc= getDistance(a, b, c, d);
    	return (long) distanc;
    }
   
    public static double getLngt(double longt1, double lat1, double distance){
        double a = (180*distance)/(PI*R*Math.cos(lat1*PI/180));
        return a;
    }
   
    public static double getLat(double longt1, double lat1, double distance){
        double a = (180*distance)/(PI*R*Math.cos(lat1*PI/180));
        return a;
    }
    /**
     * 计算   2000m 的正方形 四条边的经纬度 上纬度 下纬度 左经度 右经度
     * lng 精度
     * lat 纬度 纬度之间 距离111000m
     * distance m
     */
    public static long[] getFourLocation(Double lng, Double lat) {
    	if(lng==null){
    		lng=0.0;
    	}
    	if(lat==null){
    		lat=0.0;
    	}
    	int	distance=2000;
    	return getFourLocation(lng, lat,distance);
    }
    public static long[] getFourLocation(double lng, double lat, double distance) {
    	long [] result=new long[4];
    	
    	double latDistance=distance/111000;
    	double lngDistance=getLngt(lng, lat, distance);
    	
    	System.out.println(latDistance+"--");
    	System.out.println(lngDistance+"===");
    	
    	result[0]=(long)((lat+latDistance)*10000000000L/1L);//上纬度 
    	result[1]=(long)((lat-latDistance)*10000000000L/1L);//下纬度
    	
    	result[2]=(long)((lng+lngDistance)*10000000000L/1L);//左经度
    	result[3]=(long)((lng-lngDistance)*10000000000L/1L);//右经度
    	return result;
    }
    
    public static void main(String[] args) {
        //根据两点间的经纬度计算距离，单位： m
        System.out.println(getDistance(118.1268122112,36.6983212121, 118.0931212121, 36.6983212121));
        System.out.println(getLngt(118.11,36.68484, 1500));
        
        //中心经纬度点的 指定距离  正方形 四条边 经纬度 
        long [] result=getFourLocation(118.1268122112, 36.6983212121,1500);
        System.out.println(Arrays.toString(result));
    }
}